import { http } from "@/utils/http";

// 角色列表请求参数
export interface RoleListRequest {
  page: number;
  pageSize: number;
  keyword?: string;
}

// 角色创建请求参数
export interface RoleCreateRequest {
  name: string;
  code: string;
  description?: string;
  status?: number;
}

// 角色更新请求参数
export interface RoleUpdateRequest {
  id: number;
  name: string;
  code: string;
  description?: string;
  status?: number;
}

// 角色权限请求参数
export interface RolePermissionRequest {
  role_id: number;
  permissions: Array<{
    permission_id: number;
    auths: string[];
  }>;
}

// 角色菜单关联请求
export interface RoleMenuRequest {
  role_id: number;
  menu_ids: number[];
}

// 角色信息
export interface Role {
  id: number;
  name: string;
  code: string;
  description: string;
  status: number;
  is_system: number;
  created_at: string;
  updated_at: string;
  permissions: any[] | null;
}

// 获取角色列表
export const getRoleList = (data: RoleListRequest) => {
  return http.request<{
    code: number;
    msg: string;
    data: {
      list: Role[];
      total: number;
    };
  }>("post", "/partner/roles", { data });
};

// 创建角色
export const createRole = (data: RoleCreateRequest) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/partner/addroles", { data });
};

// 更新角色
export const updateRole = (data: RoleUpdateRequest) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/partner/rolesupdate", { data });
};

// 删除角色
export const deleteRole = (id: number) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/partner/rolesdel", { data: { id } });
};

// 获取角色权限
export const getRolePermissions = (roleId: number) => {
  return http.request<{
    code: number;
    msg: string;
    data: Array<{
      id: number;
      name: string;
      code: string;
      type: number;
      parent_id: number;
      path: string;
      icon: string;
      component: string;
      sort: number;
      status: number;
    }>;
  }>("post", "/partner/rolespermissions", { data: { role_id: roleId } });
};

// 设置角色权限
export const setRolePermissions = (data: RolePermissionRequest) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/partner/rolespermissionsadd", { data });
};

// 获取角色关联的菜单ID列表
export const getRoleMenus = (roleId: number) => {
  return http.request<{
    code: number;
    msg: string;
    data: any; // 菜单ID列表
  }>("post", "/partner/rolespermissions", { data: { role_id: roleId } });
};

// 设置角色关联的菜单
export const setRoleMenus = (data: RoleMenuRequest) => {
  return http.request<{
    code: number;
    msg: string;
  }>("post", "/partner/role/menus/set", { data });
}; 